<template>
  <el-form
    :model="dic"
    ref="dic"
    :rules="validate_rules"
    label-width="100px"
    style="width:500px"
  >
    <el-form-item label="数据编码" prop="dickeycode">
      <el-input v-model="dic.dickeycode" placeholder="输入数据编码"></el-input>
    </el-form-item>
    <el-form-item label="数据名称" prop="dickeyvalue">
      <el-input v-model="dic.dickeyvalue" placeholder="输入数据名称"></el-input>
    </el-form-item>
    <el-form-item label="数据类型" prop="dictype">
      <el-select v-model="dic.dictype" placeholder="请选择数据类型" clearable>
        <el-option
          v-for="(item, index) in sysdic_types"
          :key="index"
          :label="item.dicType"
          :value="item.id"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">{{
        !$_.isEmpty(dicId) ? "立即修改" : "立即创建"
      }}</el-button>
      <el-button @click="$router.push({ path: '/admin/sysdictionary' })"
        >取消</el-button
      >
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  data() {
    return {
      dic: { dickeycode: "", dickeyvalue: "", dictype: undefined },
      sysdic_types: [],
      dicId: null,
      validate_rules: {
        dickeycode: [
          {
            validator: (rule, value, callback) => {
              if (this.$_.isEmpty(value)) {
                callback(new Error("数据编码不能为空"));
              } else {
                callback();
              }
            },
            trigger: "change"
          }
        ],
        dickeyvalue: [
          {
            validator: (rule, value, callback) => {
              if (this.$_.isEmpty(value)) {
                callback(new Error("数据名称不能为空"));
              } else {
                callback();
              }
            },
            trigger: "change"
          }
        ],
        dictype: [
          {
            validator: (rule, value, callback) => {
              if (isNaN(value) || value == 0) {
                callback(new Error("请选择一种数据类型"));
              } else {
                callback();
              }
            },

            trigger: "change"
          }
        ]
      }
    };
  },
  methods: {
    onSubmit() {
      this.$refs["dic"].validate(valid => {
        if (valid) {
          this.saveDic();
        } else {
          return false;
        }
      });
    },
    getAllDictype() {
      this.$http.get("/sysdic/getAllDictype").then(res => {
        if (res.data.errorCode == "SUCCESS") {
          this.sysdic_types = res.data.returnData;
        } else {
          this.$message({
            type: "warning",
            message: res.data.errorMessage
          });
        }
      });
    },
    async loadDicById() {
      const res = await this.$http.post("/sysdic/getDicById", {
        id: this.dicId
      });

      if (res.data.errorCode == "SUCCESS") {
        this.dic.dickeycode = res.data.returnData.dicKeyCode;
        this.dic.dickeyvalue = res.data.returnData.dicKeyValue;
        this.dic.dictype = res.data.returnData.dicType;
      } else {
        this.$message({
          type: "warning",
          message: res.data.errorMessage
        });
      }
    },

    async saveDic() {
      /**
       * @type{SysDic}
       */
      const post_dic = {
        id: this.dicId,
        dicKeyCode: this.dic.dickeycode,
        dicKeyValue: this.dic.dickeyvalue,
        dicType: this.dic.dictype
      };
      const res = await this.$http.post("/sysdic/saveDic", post_dic);
      if (res.data.errorCode == "SUCCESS") {
        this.$message({ type: "success", message: res.data.errorMessage });
      } else {
        this.$message({
          type: "warning",
          message: res.data.errorMessage
        });
      }
    }
  },

  created() {
    this.getAllDictype();
    this.dicId = this.$route.params.id;
    if (!this.$_.isEmpty(this.dicId)) {
      this.loadDicById();
    }
  }
};
</script>

<style lang="scss" scoped>
.el-select {
  width: 100%;
}
</style>
